package shuashuami.hb.com.http;

import android.app.Activity;
import android.os.Handler;
import android.os.Message;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.share.ShareCUserInfo;

/**
 * Created by hufangda on 2017/7/11.
 */

public class ZailaiyidanHttp {

    private Activity activity;

    private String taskID;

    public ZailaiyidanHttp(Activity activity, String taskID) {
        this.activity = activity;
        this.taskID = taskID;
    }

    public void getMessage() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(activity);
        HttpBMethods.addTask(listener, shareCUserInfo.getId(), taskID);
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    try {
                        JSONObject jsonObject = new JSONObject(msg.obj.toString());
                        if (jsonObject.getInt("status") == 203) {
                            LoginHttp loginHttp = new LoginHttp(activity);
                            loginHttp.login();
                            return;
                        }
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        } else {
                            ToastUtil.showShort(activity, jsonObject.getString("message"));
                        }

                    } catch (JSONException e) {
                        ToastUtil.showShort(activity, "数据解析错误");
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };
}
